//
//  DestTravelNotesViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15/10/22.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "DestTravelNotesViewController.h"
#import "BaseHeader.h"
#import "MJRefresh.h"
#import "DestTraveNotesPhotoesViewController.h"
#import "DestImpressionsFristTableViewCell.h"
#import "DestImpressionModel.h"
@interface DestTravelNotesViewController ()<UITableViewDataSource,UITableViewDelegate,TraveNotesDelegate>
@property(strong,nonatomic)UITableView *destTravelTabelView;
@property(strong,nonatomic)NSMutableArray*destTravelTabelViewModelArray;
@end

@implementation DestTravelNotesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = NO;
    UIColor * color = [UIColor whiteColor];
    //这里我们设置的是颜色，还可以设置shadow等，具体可以参见api
    NSDictionary * dict = [NSDictionary dictionaryWithObject:color forKey: NSForegroundColorAttributeName];
    //大功告成
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.title = @"游记";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame =CGRectMake(0, 20, 25, 25);
    [button setImage:[UIImage imageNamed:@"play-jiantouzuo.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(DestGoBack) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    _destTravelTabelViewModelArray = [[NSMutableArray alloc]init];
    _start =@0;
    [self createTabelView];
    [self addFooterRefresh];
}

#pragma mark - 创建tabelkView
- (void)createTabelView {
    _destTravelTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _destTravelTabelView.dataSource = self;
    _destTravelTabelView.delegate = self;
    [self.view addSubview:_destTravelTabelView];
    
}

#pragma mark - 给tabelView加上拉刷新
- (void)addFooterRefresh {
    __block DestTravelNotesViewController *trave = self;
    [_destTravelTabelView addLegendFooterWithRefreshingBlock:^{
        // trave.start = trave.start +5;
        [trave  getTravelTabelViewdata:trave.start.integerValue];
    }];
    [_destTravelTabelView.footer beginRefreshing];
}
#pragma mark -  请求数据的方法
- (void)getTravelTabelViewdata:(NSInteger)start {
    
    [DestAFNDataBlock string:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/poi_trips/?start=%ld&count=5",_getMyType,_getMyId,start] AFNData:^(id block) {
        
        if ([[block objectForKey:@"next_start"] isKindOfClass:[NSNull class]]) {
            [_destTravelTabelView.footer endRefreshing];
            [_destTravelTabelView removeFooter];//移除加载
            return ;
        }else{
            
            _start = [block objectForKey:@"next_start"];
        }
        ///主要的tabelView赋值语句
        NSMutableArray *tripsArray = [block objectForKey:@"trips"];
        for (NSMutableDictionary * tripsDic in tripsArray) {
            DestImpressionModel *model = [DestImpressionModel modelWithDictionary:tripsDic];
            if ([model.trip_text isEqualToString:@""]) {
                model.trip_TextHeight = 0.0f;
            }else{
                model.trip_TextHeight =[StringWithHeight heightWithText:model.trip_text width:SCREEN_WIDTH - 10 *WIDTH font:[UIFont systemFontOfSize:13.0]];
            }
            [_destTravelTabelViewModelArray addObject:model];
        }
        

        [_destTravelTabelView reloadData];
        [_destTravelTabelView.footer endRefreshing];
    } AFNData1:^(id block) {

    }];
}
#pragma mark - tableView高度 返回cell数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _destTravelTabelViewModelArray.count;
}
#pragma mark -  tabelView代理 返回高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    DestImpressionModel *model = [_destTravelTabelViewModelArray objectAtIndex:indexPath.row];
    
    return (model.trip_TextHeight + 46 +170) *HEIGHT;
}
#pragma mark -  tabelView代理 返回cell种类
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"destImpressionsFirstTableViewCellTwo";
    DestImpressionsFristTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[DestImpressionsFristTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }
    DestImpressionModel *firstModel =[_destTravelTabelViewModelArray objectAtIndex:indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.dataModel = firstModel;
    cell.delegate = self;
    return cell;
}
#pragma mark - 返回按钮
- (void)DestGoBack {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)sendtrip_id:(NSNumber *)trip_id sendPhoto:(NSString *)photo {
    DestTraveNotesPhotoesViewController *traveNotesPhotoes = [[DestTraveNotesPhotoesViewController alloc]init];
    traveNotesPhotoes.trip_id = trip_id;
    traveNotesPhotoes.pushPhoto = photo;
    [self.navigationController pushViewController:traveNotesPhotoes animated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
